import sys
n = input()
earliest = "23:59:59"
latest = "00:00:00"
signIn =""
signOut =""
for i in range(n):
	name,come,go=sys.stdin.readline().split()
	if come < earliest:
		signIn = name
		earliest = come
	elif go >= latest:
		signOut = name
		latest = go
print signIn + ' ' + signOut
